package com.jnshu.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jnshu.dao.TaskMapper;
import com.jnshu.dao.UserMapper;
import com.jnshu.dao.UserTaskRelationMapper;
import com.jnshu.dto.sign.UserSignDto;
import com.jnshu.dto.task.TaskListBackList;
import com.jnshu.dto.task.TaskListDetailDto;
import com.jnshu.dto.task.TaskListDto;
import com.jnshu.dto.task.TaskVo;
import com.jnshu.exception.MyException;
import com.jnshu.pojo.Task;
import com.jnshu.pojo.User;
import com.jnshu.pojo.UserTaskRelation;
import com.jnshu.service.TaskService;
import com.jnshu.tools.binding.JsonUtil;
import com.jnshu.tools.resultUtils.PageBean;
import com.jnshu.tools.resultUtils.RUtil;
import com.jnshu.tools.resultUtils.ResultBean;
import com.jnshu.tools.resultUtils.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * @author xmp
 * @date 2019/8/9 15:47
 */
@Service
@Slf4j
public class TaskServiceImpl implements TaskService {
    @Autowired(required = false)
    TaskMapper taskMapper;
    @Autowired(required = false)
    UserMapper userMapper;
    @Autowired(required = false)
    UserTaskRelationMapper userTaskRelationMapper;

    /**
     * 查询后台任务列表
     * @param
     * @return
     */
    @Override
    public PageBean selectTaskList(TaskListBackList taskListBackList) {
        Page page = PageHelper.startPage(taskListBackList.getPageNum(), taskListBackList.getPageSize());
        List<TaskListDto> taskListDto = taskMapper.selectTaskList(taskListBackList);
        log.info("TaskList:"+ JsonUtil.toJsonString(taskListDto));
        if (taskListDto == null){
            return RUtil.errorPage(ResultEnum.NOTHINGNESS);
        }
        return RUtil.okDefault(page.getTotal(),taskListDto,taskListBackList.getPageNum(),taskListBackList.getPageSize());
    }


    @Override
    public PageBean selectTask(int pageSize,int pageNum) {
        Page page = PageHelper.startPage(pageNum,pageSize);
        List<Task> taskList = taskMapper.selectTask();
        log.info("messageList列表查询："+taskList);
        if (taskList == null){
            return RUtil.errorPage(ResultEnum.NOTHINGNESS);
        }
        return RUtil.okDefault(page.getTotal(),taskList,pageNum,pageSize);
    }

    /**
     * 查询后台任务详情
     * @param id
     * @return
     */
    @Override
    public TaskListDetailDto selectTaskById(long id) {
        Task task = taskMapper.selectTaskById(id);
        if (task == null){
            throw new MyException(401,"请求用户不存在");
        }
        TaskListDetailDto taskListDetailDto = new TaskListDetailDto();
        BeanUtils.copyProperties(task,taskListDetailDto);
        return taskListDetailDto;
    }

    /**
     * 后台任务新增
     * @param taskVo
     * @return
     */
    @Override
    public ResultBean insertTask(TaskVo taskVo) {
        Task task = new Task();
        BeanUtils.copyProperties(taskVo,task);
        long now = System.currentTimeMillis();
        task.setCreateAt(now);
        task.setUpdateAt(now);
        task.setStatus(0);
        log.info("task:"+task);
        boolean result = taskMapper.insertSelective(task);
        log.info("新增任务结果："+result);
        if (result == true){
            return RUtil.ok();
        }
        return RUtil.error(ResultEnum.FAILURE);
    }

    /**
     * 后台任务编辑
     * @param taskVo
     * @return
     */
    @Override
    public ResultBean updateTask(TaskVo taskVo) {
        log.info("TaskVo:"+taskVo);
        if (taskVo.getStatus()==1 && taskVo.getId() == null){
            return RUtil.error(ResultEnum.Message_Status_Fail);
        }
        Task task = new Task();
        BeanUtils.copyProperties(taskVo,task);
        long now = System.currentTimeMillis();
        task.setUpdateAt(now);
        task.setUpdateBy(1L);
        long id = task.getId();
        Task task1 = taskMapper.selectTaskById(id);
        log.info("查出来的ID："+id);
        if (task1 == null){
            return  RUtil.error(ResultEnum.NOTHINGNESS);
        }
        boolean result = taskMapper.updateByPrimaryKeySelective(task);
        if (result == true){
            return RUtil.ok();
        }
        return RUtil.error(ResultEnum.NOTHINGNESS);
    }

    /**
     * 后台任务删除
     * @param id
     * @return
     */
    @Override
    public ResultBean deleteTask(long id) {
        log.info("传入的ID是："+id);
        boolean result = taskMapper.deleteByPrimaryKey(id);
        log.info("ID删除成功了吗："+result);
        if (result == false) {
            log.info("公告删除失败，开始回滚");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return RUtil.error(ResultEnum.NOTHINGNESS);
        }
        return RUtil.ok();
    }

    /**
     * 后台任务状态上下架变更
     * @param id
     * @param status
     * @return
     */
    @Override
    public ResultBean updateTaskStatus(long id, int status) {
        log.info("任务ID："+id+",任务此时的状态："+status);
        if (status == 0){
            status = 1;
        }else {
            status = 0;
        }
        Task task = new Task();
        long now = System.currentTimeMillis();
        task.setId(id);
        task.setStatus(status);
        task.setUpdateAt(now);
        log.info("task:"+task);
        boolean result = taskMapper.updateByPrimaryKeySelective(task);
        if (result == true){
            return RUtil.ok();
        }
        return RUtil.error(ResultEnum.FAILURE);
    }

    /**
     * 前台日常任务查询
     * @param id
     * @return
     */
    @Override
    public ResultBean everydayTask(Long id) {
        log.info("前台用户id为："+id);
        if (id !=null) {
             List<TaskListDto> taskListDto = taskMapper.selectEverydayTask(1);
            return RUtil.ok(taskListDto);
        }else {
         log.info("前台用户id为空");
         return RUtil.error(ResultEnum.FAILURE);
        }
    }

    /**
     * 前台日常任务状态查询
     * @param id
     * @return
     */
    @Override
    public ResultBean everydayTaskStatus(Long id) {
        log.info("前台用户id为："+id);
        if (id != null){
            List<UserTaskRelation> userTaskRelation = userTaskRelationMapper.selectByUserId(id);
            log.info("根据用户id查询出来的关系数据为："+JsonUtil.toJsonString(userTaskRelation));
            Integer[] status = new Integer[3];
            for (UserTaskRelation userTaskList : userTaskRelation) {
                if (userTaskList.getTaskId()==7){
                    log.info("当userTaskList的taskId为7时的数据："+userTaskList);
                    status[0] = userTaskList.getStatus();
                }if (userTaskList.getTaskId()==8){
                    log.info("当userTaskList的taskId为8时的数据："+userTaskList);
                    status[1] = userTaskList.getStatus();
                }if (userTaskList.getTaskId()==9){
                    log.info("当userTaskList的taskId为9时的数据："+userTaskList);
                    status[2] = userTaskList.getStatus();
                }
            }
            return RUtil.ok(status);
        }else {
            log.info("用户id为空");
            return RUtil.error(ResultEnum.FAILURE);
        }
    }
}
